import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    detail: {},
    type: '',
  },

  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
        type: options.type
      })
    }
  },

  onShow: function () {
    this.getdetail()
  },

  chooseCourier(e) {
    this.setData({
      shipperCode: e.currentTarget.dataset.code,
      courierIdx: e.currentTarget.dataset.id,
      shipperName: e.currentTarget.dataset.name,
      filterCourier: []
    })
  },

  filterCourier: function (e) {
    if (this.timer) {
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(() => {
      // 在这里进行我们的操作  这样就不会频繁的进行我们这里面的操作了
      var value = e.detail.value;
      if (value != '' && value.trim().length != 0) {
        _wxRequest.wxTonkenAjax({
          url: app.globalData.apiUrl + '/sys/dict/inputSearchShipperByName',
          data: {
            name: value
          },
          method: 'POST',
        }, (res) => {
          console.log(res);
          let filterCourier = res.list
          this.setData({
            filterCourier
          })
        })
      } else {
        this.setData({
          filterCourier: []
        })
      }
    }, 600)
  },

  //获取详情
  getdetail: function () {
    console.log(typeof this.data.id)
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/detail?id=' + this.data.id,
      method: 'POST',
    }, (res) => {
      var resVo = res.vo

      this.setData({
        detail: resVo
      })
    })
  },

  //查询快递
  logisticsNo: function (e) {
    var value = e.detail.value
    this.setData({
      expNo: e.detail.value
    })
    if (value) {
      this.logisticsDist()
    }

  },


  //查询快递
  logisticsDist: function () {
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/logistics/dist?expNo=' + this.data.expNo,
      method: 'POST',
      data: {
        expNo: this.data.expNo
      }
    }, (res) => {
      if (res.vo) {
        that.setData({
          shipperName: res.vo.shipperName,
          shipperCode: res.vo.shipperCode
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: '快递单号无法识别'
        })
      }

    })
  },

  // getShipperName(e) {
  //   let name = e.detail.value
  //   let url = '/sys/dict/getShipperByName?name=' + name
  //   wx.request({
  //     url: app.globalData.apiUrl + url,
  //     method: 'post',
  //     header: {
  //       token: wx.getStorageSync('Authorization')
  //     },
  //     success:(res) => {
  //       console.log(res, 123123);
  //       this.data.shipperCode = res.data.code
  //       this.data.shipperName = res.data.name
  //     }
  //   })
  // },

  //发货
  formSubmit: function (e) {
    // var params = this.data.detail || {}
    var params = {}
    console.log(params, 'params')
    var formdata = e.detail.value

    params.id = Number(this.data.id)
    params.shipperName = formdata.shipperName || this.data.shipperName
    params.shipperCode = this.data.shipperCode
    params.expressNo = this.data.expNo

    if (formdata.expressNo == '') {
      wx.showToast({
        icon: 'none',
        title: '快递单号不能为空',
      })
      return
    }

    if (formdata.shipperName == '') {
      wx.showToast({
        icon: 'none',
        title: '快递名称不能为空',
      })
      return
    }
    var url = '/odr/protected/order/sendOutGoods'
    // 采购发货
    // if(this.data.type==2){
    //   url = '/odr/protected/orgOrder/sendOutGoods'
    // }
    console.log('params', JSON.stringify(params));
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: params
    }, (res) => {
      console.log(res, '发货成功')
      wx.showToast({
        title: '发货成功'
      })
      setTimeout(() => {
        wx.navigateBack({})
      }, 1500)
    })


  }





})